﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using static SY_Library.User32;

namespace ShortcutMenu.Hooks
{
    public class DesktopHook : HookBase
    {
        public const int WM_KEYDOWN = 0x0100;
        public const int WH_KEYBOARD_LL = 13;
        public const int WM_SYSKEYDOWN = 0x0104;
        private const int WM_MOUSEMOVE = 0x200;
        private const int WM_LBUTTONDOWN = 0x201;
        private const int WM_RBUTTONDOWN = 0x204;
        private const int WM_MBUTTONDOWN = 0x207;
        private const int WM_LBUTTONUP = 0x202;
        private const int WM_RBUTTONUP = 0x205;
        private const int WM_MBUTTONUP = 0x208;
        private const int WM_LBUTTONDBLCLK = 0x203;
        private const int WM_RBUTTONDBLCLK = 0x206;
        private const int WM_MBUTTONDBLCLK = 0x209;

        public const int WH_MOUSE_LL = 14; // mouse hook constant

        //钩子回掉委托实例
        private static HookHandlerDelegate proc;

        //钩子句柄
        private static IntPtr hKeyboardHook;

        //任务回调
        DesktopAction _desktopAction;


        public DesktopHook(MainWindow mainWindow) : base(mainWindow)
        { }

        ~DesktopHook()
        {
            HookStop();
        }

        private int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            // 假设正常执行而且用户要监听鼠标的消息
            if (nCode >= 0)
            {
                MouseHookStruct MyMouseHookStruct;
                switch (wParam)
                {
                    case WM_LBUTTONDOWN://左键按下
                        MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct))!;
                        //_desktopAction.LBUTTONDOWN(MyMouseHookStruct);
                        break;
                    case WM_LBUTTONUP://左键抬起
                        MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct))!;
                        //_desktopAction.LBUTTONUP(MyMouseHookStruct);
                        break;
                    case WM_LBUTTONDBLCLK:
                        break;
                    case WM_RBUTTONDOWN://右键按下
                        MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct))!;
                        //_desktopAction.RBUTTONDOWN(MyMouseHookStruct);
                        break;
                    case WM_RBUTTONUP://右键抬起
                        MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct))!;
                        _desktopAction.RBUTTONUP(MyMouseHookStruct);
                        break;
                    case WM_RBUTTONDBLCLK:
                        break;
                }
            }

            // 启动下一次钩子
            return CallNextHookEx((int)hKeyboardHook, nCode, wParam, lParam);
        }

        public new void HookStart(DesktopAction desktopAction)
        {
            // 安装鼠标钩子
            if (hKeyboardHook == IntPtr.Zero)
            {
                // 创建HookProc实例 
                proc = new HookHandlerDelegate(MouseHookProc);
                using (Process curPro = Process.GetCurrentProcess())
                using (ProcessModule curMod = curPro.MainModule)
                {
                    //定义全局钩子 
                    hKeyboardHook = SetWindowsHookEx(
                        WH_MOUSE_LL,
                        proc,
                        SY_Library.Kernel32.GetModuleHandle(curMod.FileName),
                        0);
                    _desktopAction = desktopAction;
                }

                if (hKeyboardHook == IntPtr.Zero)
                {
                    HookStop();
                    throw new Exception("钩子设置失败");
                }
            }
        }

        public override void HookStop()
        {
            bool retKeyboard = true;
            if (hKeyboardHook != IntPtr.Zero)
            {
                retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
                hKeyboardHook = IntPtr.Zero;
            }
            if (!(retKeyboard)) throw new Exception("卸载钩子失败");
        }


        public class DesktopAction
        {
            public Action<MouseHookStruct> LBUTTONDOWN { get; set; }
            public Action<MouseHookStruct> LBUTTONUP { get; set; }
            public Action<MouseHookStruct> RBUTTONDOWN { get; set; }
            public Action<MouseHookStruct> RBUTTONUP { get; set; }
        }

        #region MyRegion
        public delegate int HookHandlerDelegate(int nCode, Int32 wParam, IntPtr lParam);

        // 装载钩子
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern IntPtr SetWindowsHookEx(int idHook, HookHandlerDelegate lpfn, IntPtr hmod, uint dwThreadID);

        /// <summary>
        /// 位置
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public class POINT
        {
            public int x;
            public int y;
        }

        /// <summary>
        /// 钩子结构体
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public class MouseHookStruct
        {
            public POINT pt;
            public int hWnd;
            public int wHitTestCode;
            public int dwExtraInfo;
        }
        #endregion
    }
}
